//
//  Register_phoneViewController.m
//  帮惠Mall
//
//  Created by reew on 2017/9/15.
//  Copyright © 2017年 reew. All rights reserved.
//

#import "Register_phoneViewController.h"
#import "CommonUtils.h"
#import <SVProgressHUD.h>
#import "Register_codeViewController.h"
#import "CommonUtils.h"
#import <AFNetworking.h>
#import "Login.h"
#import "Account.h"
#import "System.h"
@interface Register_phoneViewController ()
@property (weak, nonatomic) IBOutlet UIButton *closeBtn;
@property (weak, nonatomic) IBOutlet UITextField *phoneNum;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (nonatomic,copy) NSString *memberNo;
@property (nonatomic,copy) NSString *adminNo;
@end

@implementation Register_phoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
    leftLabel.text = @"+86";
    leftLabel.font = [UIFont systemFontOfSize:18];
    _phoneNum.leftViewMode = UITextFieldViewModeAlways;
    _phoneNum.leftView = leftLabel;
    self.navigationController.navigationBarHidden = YES;
}
//获取验证码

- (void)getAuthCode_admin{
    AFHTTPSessionManager *manager = [CommonUtils getAFHTTPSessionManager];
    NSString *urlStr = [kBaseURL stringByAppendingPathComponent:kGetAuthCode_admin];
    [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        //参数 手机号：mobile
        [formData appendPartWithFormData:[_phoneNum.text dataUsingEncoding:NSUTF8StringEncoding] name:@"mobile"];
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@",responseObject);
        //根据返回信息 做相应处理
        NSNumber *status = responseObject[@"result"];
        NSString *message = responseObject[@"message"];
        if ([status integerValue] == 1) {
            //请求成功，保存memberNo
            [Account sharedAccount].adminNo = responseObject[@"adminNo"];
            [self performSegueWithIdentifier:@"getAuthCode" sender:self];
        }else{
            [SVProgressHUD showInfoWithStatus:message];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}
- (IBAction)close:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    Register_codeViewController *vc = [segue destinationViewController];
    vc.phone = _phoneNum.text;
    vc.memberNo = _memberNo;
}
-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    
    if ([CommonUtils isPhoneNum:_phoneNum.text]) {
        [self getAuthCode_admin];

    }else{
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码"];
    }
    return NO;
}
@end
